<?php

namespace api\modules\app\services;

use api\modules\app\models\TypeSync;
use api\modules\manage\models\DictElements;
use Yii;
use yii\helpers\ArrayHelper;

class MetaService
{
    const CODE_CACHE_KEY = 'paidan30_code';

    public function getCodeDict()
    {
        $cache = Yii::$app->redisCache;
        $data = $cache->get(self::CODE_CACHE_KEY);
        if (!empty($data)) {
            return json_decode($data, true);
        }
        $data = [];
        $elements = DictElements::findAll(['state' => '1']) ?? [];
        foreach ($elements as $element) {
            $data[$element->code] = $element->name;
        }
        $cache->set(self::CODE_CACHE_KEY, json_encode($data), 24*3600);

        return $data;
    }

    public function getLinkEvents()
    {

    }

    public static function hardcodeEventTypes1()
    {
        $result = [];
        $xhs = TypeSync::findOne(['name' => '消火栓']);
        $result[] = $xhs;
        $data = TypeSync::findAll(['status' => 1]);
        foreach ($data as $row){
            if($row->name !== '消火栓'){
                $result[] = $row;
            }
        }
        return $result;
    }

    public static function hardcodeEventTypes() {
        $data = TypeSync::find()->where(['status' => 1])->groupBy('event_type3')->indexBy('event_type3')->asArray()->all();
        //消火栓(专业)放第一个
        if (isset($data['消火栓(专业)'])) {
            $result[] = $data['消火栓(专业)'];
        }

        foreach ($data as $k => $row) {
            if ($k == '消火栓(专业)') {
                continue;
            }
            $result[] = $row;
        }

        return $result;
    }

//    public static function hardcodeEventypes()
//    {
//        $data = TypeSync::findAll(['status' => 1]);
//        return $data;
//    }

    public static function getManageDeps()
    {
        $data = TypeSync::find()->andFilterWhere(['status' => 1])->select(['manage_dep'])->distinct()->asArray()->all();
        $data = ArrayHelper::getColumn($data,'manage_dep');
        return $data;
    }
}

